﻿using System;
using System.IO;
using System.Windows;

namespace Engine.Helpers
{
    public static class LogWriter
    {
        public static bool Enable { get; set; }
        public static int LogLevel { get; set; }
        private static StreamWriter sw;

        public static void SetFilename(string filename)
        {
            LogLevel = 0;
            try
            {
                sw = new StreamWriter(filename);
                LogWriter.Enable = true;
                sw.AutoFlush = true;
                Write(0, "filename: " + filename);
#if DEBUG
                MessageBox.Show(filename);
#endif
            }
            catch (Exception ex)
            {
                LogWriter.Enable = false;
                Console.WriteLine(ex.Message);
                MessageBox.Show(ex.Message);
            }
        }

        public static void Write(string message)
        {
            Write(0, message);
        }

        public static void Write(int loglevel, string message)
        {
            if (LogWriter.Enable)
            {
                if (loglevel >= LogWriter.LogLevel)
                {
                    //MessageBox.Show(message);
                    LogWriter.sw.WriteLine(DateTime.Now.ToLocalTime() + " " + message);
                }
            }
        }
    }
}